package IO.xml.comprehension;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import librairies.liste.interfaces.IListeE;
import librairies.liste.list.ListeDCE;
import modele.Mot;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.DefaultHandler;

import enumeration.type.ETypeAction;

public class ComprehensionHandler extends DefaultHandler
{
	private File file;
	
	private IListeE<Mot> liste;
	private Mot mot;

	private String provenance;
	private StringBuffer buffer;

	public ComprehensionHandler(File fich)
	{
		super();
        this.file = fich;
	}
	
	public ComprehensionHandler()
	{
		super();
	}
	
	/**
	 * lancement de la lecture
	 * @return le fichier xml.
	 */
	public IListeE<Mot> lecture() 
	{
		try
		{
			SAXParserFactory fabrique = SAXParserFactory.newInstance();
			SAXParser parseur;
			parseur = fabrique.newSAXParser();
			
			parseur.parse(this.file, this);
		}
		catch(ParserConfigurationException e)
		{
			e.printStackTrace();
			return null;
		}
		catch(SAXException e)
		{
			e.printStackTrace();
			return null;
		}
		catch(IOException e)
		{
			e.printStackTrace();
			return null;
		}

		return this.liste;
	}
	
	/**
	 * aucune idee.
	 */
	public ContentHandler ecriture(ContentHandler handler, AttributesImpl atts) throws SAXException 
	{	
		return handler;
	}
	
	/**
	 * debut d'une balise.
	 */
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
	{
		if(qName.equals("Dico"))
		{
			this.liste = new ListeDCE<Mot>();
		}
		else if(qName.equals(ETypeAction.RECHERCHE.getValue()))
		{
			this.mot = new Mot();
			this.provenance = ETypeAction.RECHERCHE.getValue();
			this.mot.setProvenance(this.provenance);
		}
		else if(qName.equals(ETypeAction.COPIE.getValue()))
		{
			this.mot = new Mot();
			this.provenance = ETypeAction.COPIE.getValue();
			this.mot.setProvenance(this.provenance);
		}
		else if(qName.equals(ETypeAction.OUVERTURE.getValue()))
		{
			this.mot = new Mot();
			this.provenance = ETypeAction.OUVERTURE.getValue();
			this.mot.setProvenance(this.provenance);
		}
		else if(qName.equals(ETypeAction.CALCULE.getValue()))
		{
			this.mot = new Mot();
			this.provenance = ETypeAction.CALCULE.getValue();
			this.mot.setProvenance(this.provenance);
		}
		else if(qName.equals("Fichier"))
		{
			this.mot = new Mot();
			this.provenance = "Fichier";
			this.mot.setProvenance(this.provenance);
		}
		else if(qName.equals("Dossier"))
		{
			this.mot = new Mot();
			this.provenance = "Dossier";
			this.mot.setProvenance(this.provenance);
		}
		else if(qName.equals("Dans"))
		{
			this.mot = new Mot();
			this.provenance = "Dans";
			this.mot.setProvenance(this.provenance);
		}
		else if(qName.equals("Mon"))
		{
			this.mot = new Mot();
			this.provenance = "Mon";
			this.mot.setProvenance(this.provenance);
		}
		else if(qName.equals("Ton"))
		{
			this.mot = new Mot();
			this.provenance = "Ton";
			this.mot.setProvenance(this.provenance);
		}
		else if(qName.equals("Le"))
		{
			this.mot = new Mot();
			this.provenance = "Le";
			this.mot.setProvenance(this.provenance);
		}
		else if(qName.equals("Internet"))
		{
			this.mot = new Mot();
			this.provenance = "Internet";
			this.mot.setProvenance(this.provenance);
		}
		else if(qName.equals(ETypeAction.EXECUTER.getValue()))
		{
			this.mot = new Mot();
			this.provenance = ETypeAction.EXECUTER.getValue();
			this.mot.setProvenance(this.provenance);
		}
		else if(qName.equals("Argument"))
		{
			this.mot = new Mot();
			this.provenance = "Argument";
			this.mot.setProvenance(this.provenance);
		}
		else 
		{
			this.buffer = new StringBuffer();
			if(qName.equals("Value"))
			{
			}
			else
			{
				throw new SAXException("Balise "+qName+" inconnue.");
			}
		}
	}
	
	/**
	 * fin d'une balise.
	 */
	public void endElement(String uri, String localName, String qName) throws SAXException
	{
		if(qName.equals("Dico"))
		{
		}
		else if(qName.equals("Recherche") || qName.equals("Copie") || qName.equals("Ouverture") ||
				qName.equals("Calcule") || qName.equals("Fichier") || qName.equals("Dossier") ||
				qName.equals("Dans") || qName.equals("Mon") || qName.equals("Ton") ||
				qName.equals("Le") || qName.equals("Internet") || qName.equals("Executer") ||
				qName.equals("Argument"))
		{
		}
		else 
		{
			if(qName.equals("Value"))
			{
				this.mot.setValue(buffer.toString());
				this.liste.add(this.mot);
				
				this.provenance = this.mot.getProvenance();
				this.mot = new Mot();
				this.mot.setProvenance(this.provenance);
			}
			else
			{
				throw new SAXException("Balise "+qName+" inconnue.");
			} 
		}        
	}
	
	/**
	 * mise de l'element dans le buffer.
	 */
	public void characters(char[] ch,int start, int length) throws SAXException
	{
		String lecture = new String(ch,start,length);
		if(this.buffer != null) buffer.append(lecture);       
	}

	/**
	 * aucune idee
	 */
	public void startDocument() throws SAXException {}
	public void endDocument() throws SAXException {}
}